package fr.ul.ade.prime.ui;

import fr.ul.ade.prime.model.Schedule;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

/**
 * @author		Schnell Michaël
 * @version		1.0
 */
public class AdeWebNotification implements BundleExtraNames {
	
	@SuppressWarnings("unused")
	private static final String TAG = AdeWebNotification.class.getSimpleName() ;

	private static final int SCHEDULE_UPDATE_ID = 1 ;
	
	private static int scheduleNotificationNumber ;
	
	static {
		scheduleNotificationNumber = 1 ; 
	}
	
	private AdeWebNotification() {
		super() ;
	}// AdeWebNotification()
	
	public static void initScheduleNotificationNumber() {
		scheduleNotificationNumber = 1 ;
	}// initScheduleNotificationNumber()
	
	public static void dispatchNotification(Context context, int iconId, 
			CharSequence tickerText, long when, CharSequence contentTitle, 
			CharSequence contentText, Intent notificationIntent, 
			int notificationDefaults, int notificationFlags,
			int notificationId, int notificationNumber) {
		// Retrieve Notification manager
		String ns = Context.NOTIFICATION_SERVICE ;
		NotificationManager mNotificationManager = 
				(NotificationManager) context.getSystemService(ns) ;

		// Initialize notification
		Notification notification = new Notification(iconId, tickerText, when) ;
		PendingIntent contentIntent = PendingIntent.getActivity(
					context, 0, notificationIntent, 0) ;
		notification.setLatestEventInfo(
				context, contentTitle, contentText, contentIntent) ;
		notification.defaults |= notificationDefaults ;
		notification.flags |= notificationFlags ;
		notification.number = notificationNumber ;
		
		// Dispatch notification
		mNotificationManager.notify(notificationId, notification) ;
	}// dispatchNotification()

	public static void makeTestNotification(Context context) {
		dispatchNotification(context, android.R.drawable.ic_dialog_alert, "AdeWebLite notification !",  //$NON-NLS-1$
				System.currentTimeMillis(), "My notification", "Hello AdeWebLite user !", new Intent(context, ScheduleList.class),  //$NON-NLS-1$ //$NON-NLS-2$
				Notification.DEFAULT_LIGHTS, Notification.FLAG_AUTO_CANCEL, SCHEDULE_UPDATE_ID, 1) ;
		dispatchNotification(context, android.R.drawable.ic_dialog_alert, "AdeWebLite notification !",  //$NON-NLS-1$
				System.currentTimeMillis(), "My notification", "Hello again AdeWebLite user !", new Intent(context, ScheduleList.class),  //$NON-NLS-1$ //$NON-NLS-2$
				Notification.DEFAULT_LIGHTS, Notification.FLAG_AUTO_CANCEL|Notification.FLAG_SHOW_LIGHTS, SCHEDULE_UPDATE_ID, 2) ;
	}// makeTestNotification(Context)
	
	public static void makeScheduleNotification(Context context, Schedule schedule) {
		Intent notificationIntent = new Intent(context, ViewSchedule.class) ;
//		Log.d(TAG, String.format("Schedule: %s (%d - %s)",
//				schedule.getName(), 
//				Integer.valueOf(schedule.getId()),
//				schedule.getState())) ;
		notificationIntent.putExtra(EXTRA_SCHEDULE, schedule) ;
		notificationIntent.putExtra(EXTRA_SCHEDULE_UPDATE_SCHEDULED, false) ;
		
		//TODO Remove hard-coded strings
		dispatchNotification(context, 
				android.R.drawable.ic_dialog_alert, 
				String.format("%s modifié", schedule.getName()),  //$NON-NLS-1$
				System.currentTimeMillis(), 
				schedule.getName(), 
				"Modification d'un cours dans les prochains jours.", //$NON-NLS-1$
				notificationIntent,  
				Notification.DEFAULT_LIGHTS, 
				Notification.FLAG_AUTO_CANCEL, 
				SCHEDULE_UPDATE_ID, 
				scheduleNotificationNumber) ;
		scheduleNotificationNumber++ ;
	}// makeScheduleNotification(Schedule)
	
}// class AdeWebNotification